package com.example.demo.config;

import com.example.demo.AiService.ConsultantService;
import com.example.demo.repository.RedisChatMemoryStore;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.ClassPathDocumentLoader;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.spring.AiService;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class CommonConfig {
    @Resource
    private OpenAiChatModel model;
    //注入聊天记忆存储对象
    @Autowired
    private ChatMemoryStore redisChatMemoryStore;
//    @Autowired
//    private EmbeddingStore store;
/*
    @Bean
    public ConsultantService consultantService() {
        ConsultantService consultantService = AiServices.builder(ConsultantService.class)
                .chatModel(model)
                .build();
        return consultantService;
    }
*/

    @Bean
    public ChatMemoryProvider chatMemoryProvider() {
        ChatMemoryProvider chatMemoryProvider = new ChatMemoryProvider(){
            @Override
            public ChatMemory get(Object memoryId) {
                return MessageWindowChatMemory.builder()
                        .id(memoryId)
                        .chatMemoryStore(redisChatMemoryStore )
                        .maxMessages(10)
                        .build();
            }
        };
        return chatMemoryProvider;
    }

/*    @Bean
    public List<Document> fileSystemDocumentLoader() {
        String path = "E:\\workspace\\LangChain4J-demo\\src\\main\\resources\\content";
        List<Document> documents = FileSystemDocumentLoader.loadDocuments(path);
        return documents;
    }*/

    /**
     * 构建向量数据库操作对象
     * @return
     */
    @Bean
    public EmbeddingStore storeA() {
        //1.加载文档进内存
        List<Document> documents =ClassPathDocumentLoader.loadDocuments("content");

        //2.构建向量数据库操作对象
        InMemoryEmbeddingStore store = new InMemoryEmbeddingStore();

        //3.构建一个EmbeddingStoreIngestor对象，完成文本切割，批量向量数据库添加文档
        EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor.builder()
                .embeddingStore(store)
                .build();
        //4.批量向量数据库添加文档
        ingestor.ingest(documents);


        return store;
    }

    /**
     * 构建内容检索器对象
     * @param store
     * @return
     */
    @Bean
    public ContentRetriever contentRetriever(@Qualifier("storeA") EmbeddingStore store) {
        EmbeddingStoreContentRetriever contentRetriever = EmbeddingStoreContentRetriever.builder()
                .embeddingStore(store)
                .minScore(0.5) //匹配度
                .maxResults(3) //最多返回3条数据
                .build();
        return contentRetriever;
    }

}
